package com.dao.impl;

import com.dao.ReturnnDao;
import com.db.ConnectionFactory;
import com.entry.Returnn;
import com.entry.Sales;
import com.util.PageModel;

import java.sql.*;
import java.util.ArrayList;


public class ReturnnDaoImpl implements ReturnnDao {
    @Override
    public int add(Returnn returnn) {
        int row = 0;
        Connection con = null;
        PreparedStatement pre = null;
        String sql = "insert into returnn(cuname,photo,reason,pname,gid,timee,countt) values(?,?,?,?,?,NOW(),?)";
        try {
            con = ConnectionFactory.getConnection();
            pre = con.prepareStatement(sql);
            pre.setString(1, returnn.getCuname());
            pre.setString(2, returnn.getPhoto());
            pre.setString(3, returnn.getReason());
            pre.setString(4, returnn.getPname());
            pre.setString(5, returnn.getGid());
            pre.setInt(6, returnn.getCountt());
            row = pre.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pre != null)
                    pre.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return row;
    }

    @Override
    public PageModel<Returnn> find(int pageSize, int pageNo, String cuname, int stacount, int endcount) {
        String sql = "select cuname,photo,reason,pname,gid,timee,countt from returnn";
        if (!("".equals(cuname) || cuname == null)) {
            sql += " where cuname like'%" + cuname + "%'";
            if (stacount != 0 && endcount != 0) {
                sql += " and countt between " + stacount + " and " + endcount;
            }
        } else if (stacount != 0 && endcount != 0) {
                sql += " where countt between " + stacount + " and " + endcount;
        }

        ArrayList<Returnn> reginfos = new ArrayList<Returnn>();
        PageModel<Returnn> pm = new PageModel<Returnn>(pageSize, pageNo,
                total(sql));

        Connection con = null;
        PreparedStatement pre = null;
        ResultSet rs = null;
        try {
            con = ConnectionFactory.getConnection();
            pre = con.prepareStatement(sql + " limit " + (pm.getPageNo() - 1)
                    * pm.getPageSize() + "," + pm.getPageSize());
            rs = pre.executeQuery();
            while (rs.next()) {
                Returnn returnn = new Returnn();
                returnn.setCuname(rs.getString(1));
                returnn.setPhoto(rs.getString(2));
                returnn.setReason(rs.getString(3));
                returnn.setPname(rs.getString(4));
                returnn.setGid(rs.getString(5));
                returnn.setTimee(rs.getString(6));
                returnn.setCountt(rs.getInt(7));
                reginfos.add(returnn);
            }
            pm.setData(reginfos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pre != null)
                    pre.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return pm;
    }

    @Override
    public int total(String sql) {
        int recordCount = 0;
        String countsql = "select count(*) "
                + sql.substring(sql.indexOf("from"), sql.length());
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            con = ConnectionFactory.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(countsql);
            if (rs.next()) {
                recordCount = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return recordCount;
    }
}
